/**
 * 表格模块
 */
layui.define(['jquery', 'form', 'table', 'laydate', 'layer', 'bmsglobal', 'bmsrdm'], function (exports) {
    var $ = layui.$;
    var bmsrdm = layui.bmsrdm;
    var form = layui.form;
    var table = layui.table;
    var layer = layui.layer;
    var MOD_NAME = 'bmsform';

    var bmsform = {
        config: {
            elems: {
                formId: "BMS-dialogForm"
                , formSubmitFilter: "BMS-dialogFormFilter"
                , submitUrl: '-'
            }
        }
    }

    var call = {
        render: function (options) {
            // form元素渲染
            form.render();
        }
        /**
         * 监听
         * @param options
         * @param active
         */
        , monitor: function (options, active) {
            var isSubmit = false;
            //监听提交
            form.on('submit(' + options.formSubmitFilter + ')', function (data) {
                if (isSubmit)
                    return false;
                isSubmit = true;
                var $formElem = $('#' + options.formId);
                // 禁用按钮
                $formElem.find('button[lay-filter=' + options.formSubmitFilter + ']').addClass('layui-btn-disabled');

                // 处理离线数据
                active.offdata(data);

                if (active.submitBefore)// 自定义提交前执行的方法
                    active.submitBefore(data);

                if (data.field.isnav == 1)
                    data.field.target = 'dialog';
                else if (data.field.isnav == 0)
                    data.field.target = 'ajax';
                $.post(options.submitUrl, data.field, function (resp) {
                    if (active.submitAfter)// 自定义提前完成后的方法
                        active.submitAfter(data, resp);

                    active._submitAfter(data, resp);
                })
                return false;
            });

            // 监听表单事件
            form.on('select', function(data){
                active.filterHander(data, 'select');
            })
            form.on('radio', function(data){
                active.filterHander(data, 'radio');
            })
            form.on('checkbox', function(data){
                active.filterHander(data, 'checkbox');
            })
            form.on('switch', function(data){
                active.filterHander(data, 'switch');
            })

            // 增加 text password 和 textarea 的 input focus blur事件
            $('#' + options.formId).find('input[type=text],input[type=password],textarea')
                .on('input focus blur', function(e)
            {
                var elem = e.target, eventType =e.type, type = e.target.type, value = e.target.value;
                var data = {elem:elem, value: value, othis: elem}
                active.filterHander(data, type, eventType);
            })
        }
    }

    /**
     * 所有操作
     * @type {{}}
     */
    bmsform.active = function (options) {
        var activeThat = this;
        var $formElem = $('#' + options.formId);
        this.active = {
            offdata: function (data) {
                // switch 开关未选中时获取 'lay-off-value' 的值
                layui.each($formElem.find('input[lay-skin=switch]'), function (index, item) {
                    var $item = $(item);
                    var name = $item.attr('name');
                    if (data.field[name] == undefined)
                        data.field[name] = $item.attr('lay-off-value');
                })
            }
            /**
             * 提交前执行
             * 可处理数据，执行一些提交前的操作
             * @param data data.field 可取得 form表单数据
             * @param resp 响应数据
             */
            , submitBefore: function (data, resp) {
                activeThat.active._submitAfter(data, resp);
            }
            , _submitAfter: function (data, resp) {
                if (!resp || resp[BMS.config.keys.codeName] != BMS.config.code.succ)
                    return;
                var index = parent.layui.layer.getFrameIndex(window.name);
                parent.layui.layer.msg('操作成功', {offset: 't', time: 2000, icon: 6});
                parent.layui.layer.close(index);
                // 重载表单
                parent.layui.bmstable.reload();
            }
            , submitAfter: function(data, resp)
            {
            }
            /**
             * 过滤事件处理器，将不同元素的事件分发到相应的方法
             * @param data
             * @param elemType
             */
            , filterHander: function(data, elemType, eventType)
            {
                var filter = $(data.elem).attr('lay-filter');
                var activeName = elemType;
                if (filter)
                    activeName += '_'+filter;

                if (eventType)// 增加事件类型
                    data.event = eventType;

                if (activeThat.active[activeName])
                    activeThat.active[activeName](data);
                else if (activeThat.active[elemType])
                    activeName.active[elemType](data);
            }
        }

        return this.active;
    }

    bmsform.init = function () {
        // 初始化事件

    }


    bmsform.init();

    bmsform.render = function (options, actives, verifys) {
        options = options || {}, actives = actives || {}, verifys = verifys || {};
        // 绑定定制的配置
        options = $.extend(bmsform.config.elems, options);
        // 绑定定制的事件
        actives = $.extend(bmsform.active(options), actives);

        // 渲染
        call.render(options);

        // 监听
        call.monitor(options, actives);

        // 添加验证
        form.verify(verifys);
    }


    exports(MOD_NAME, bmsform);
})